package org.agile4j.architecture.front.web;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.agile4j.architecture.customermgr.service.ICustomerService;
import org.agile4j.architecture.customermgr.vo.CustomerModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.common.base.Strings;

@Controller
@RequestMapping("/")
public class LoginContrller {

	@Autowired
	private ICustomerService customerService ;
	
	@RequestMapping(value="/toLogin", method=RequestMethod.GET)
	public String toLogin() {
		return "login" ;
	}
	
	@RequestMapping(value="/login", method=RequestMethod.POST)
	public String login(@RequestParam("customerId")String customerId, @RequestParam("pwd")String pwd,HttpServletResponse response){
		if (Strings.isNullOrEmpty(customerId)) {
			return "login" ;
		}
		CustomerModel cm = this.customerService.getByCustomerId(customerId);
		if (cm == null) {
			return "login" ;
		}
		Cookie cookie = new Cookie("MyLogin", cm.getUuid() +","+System.currentTimeMillis()) ;
		response.addCookie(cookie);
		return "redirect:/toIndex";
	}
}
